package cn.fx.org.controller;

import cn.fx.basic.annotation.PreAuthorize;
import cn.fx.basic.util.JsonResult;
import cn.fx.basic.util.PageList;
import cn.fx.org.domain.Department;
import cn.fx.org.query.DepartmentQuery;
import cn.fx.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类: 部门
 */
@RestController  //controller层，返回json格式数据
@RequestMapping("/department")
//接口文档的注解
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 接口: 查询所有
     * http://localhost:8080/department --- url
     * 动词: GET
     * @return
     */
    // @RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping //上面的结合
    @ApiOperation(value = "查询所有部门信息" ) //打在方法上，对方法做描述
    @PreAuthorize(name = "查询所有",sn = "department:getAll")
    public List<Department> findAll() {

        return departmentService.loadAll();
    }

    /**
     * 接口: 查询单个对象
     *  http://localhost:8080/department/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询" )
    @PreAuthorize(name = "根据id获取",sn = "department:get")
    public Department findOne(@PathVariable("id") Long id) {

        return departmentService.loadById(id);
    }

    /**
     * 接口: 分页查询
     * post - 参数通过json对象传递
     *
     * @RequestBody: 从请求体中获取数据赋值给departmentQuery
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口" )
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query) {
        try {
            return departmentService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 接口: 删除
     * @param id
     * @return
     *   JsonResult: 链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除" )
    @PreAuthorize(name ="根据id删除",sn = "department:delete")
    public JsonResult remove(@PathVariable("id") Long id) {
        try {
            departmentService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常,稍后重试");
        }

    }

    /**
     *  http://localhost:8080/department
     * 接口: 添加和修改
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    @PreAuthorize(name ="添加或者修改",sn = "department:addOrUpdate")
    public JsonResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() == null){
                //添加操作
                departmentService.save(department);
            } else {
                //修改操作
                departmentService.update(department);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            departmentService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常,稍后重试");
        }
    }

    /**
     * 获取部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树-无限极接口")
    @PreAuthorize(name = "部门列表",sn = "department:tree") //sn后面用来做权限校验
    public List<Department> deptTree(){

        return departmentService.deptTree();
    }
}
